
package com.tianyang.yukuaifuzs.base;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;

import com.tianyang.yukuaifuzs.common.cache.CacheManage;
import com.tianyang.yukuaifuzs.common.cache.CacheModel;
import com.tianyang.yukuaifuzs.utils.AppContextUtil;
import com.tianyang.yukuaifuzs.utils.PublicProviderUtil;
import com.tianyang.yukuaifuzs.utils.permissions.PermissionsMgr;

import java.util.List;

/**
 * Description : 所有类基类，提供一些公共方法
 */
public abstract class BaseActivity extends PermissionActivity {

    private final String TAG = this.getClass().getName();

    protected Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(null);
        mContext = this;
        AppContextUtil.pushActivity(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        CacheManage.getInstance().saveCache(CacheModel.ISFOREGROUND, true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        CacheManage.getInstance().saveCache(CacheModel.ISFOREGROUND, false);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    public void onPermissionDenied(List<String> permission) {
        if (permission != null && !permission.isEmpty()) {
            String perm = checkPermission(permission.toArray(new String[permission.size()]));
            if (perm != null) {
                ICommonProvider commonProvider = PublicProviderUtil.getProvider(ICommonProvider.class);
                if (commonProvider != null) {
                    commonProvider.openAppPermission(BaseActivity.this, perm, false);
                }
            }
        }
    }

    @Override
    public void onPermissionGranted(List<String> permissions) {

    }

    @Override
    public void requestPermissions(String... permissions) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (!PermissionsMgr.getInstance().hasAllPermissions(this, permissions)){
                super.requestPermissions(permissions);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppContextUtil.popActivity(this);
    }

    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
    }
}
